#include "browserview.h"
#include "ui_browserview.h"


#include "downloadHeaders/downloadviewh/downloadmanagerwidget.h"

#include "popwidget.h"
#include "settingwidget.h"
#include <QFileDialog>

#include "login.h"
#include "userconf.h"
#include "register.h"

browserview::browserview(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::browserview)
{
    ui->setupUi(this);

    this->setWindowIcon(QIcon(":/resource/imgs/icon.jpg"));
    this->setWindowTitle("WaterFox");
    ui->tab->setUrl(QUrl("https://so.lenovo.com.cn/"));
    ui->tab->resize(800,600);
    //bptr=this;
    st.ui=ui;
    initMenu();
    //设计信号

    //后退
    connect(ui->back,&QPushButton::clicked,ui->tab,[=](){
        ui->tab->back();
    });
    //前进
    connect(ui->forward,&QPushButton::clicked,ui->tab,[=](){
        ui->tab->forward();
    });
    //刷新
    connect(ui->renew,&QPushButton::clicked,ui->tab,[=](){
        ui->tab->renew();
    });
    //主页
    connect(ui->home,&QPushButton::clicked,ui->tab,[=](){
        ui->tab->setUrl(QUrl("https://so.lenovo.com.cn/"));
    });


    //新建页面
    connect(ui->newTabButton,&QPushButton::clicked,ui->tab,[=](){
        ui->tab->createTab();
    });
    //输入网址
    connect(ui->urlLine,&QLineEdit::returnPressed,ui->tab,[=](){
        QString s=ui->urlLine->text();
        if(s.first(4)!="http")
        {
            s="http://"+s;
        }
        ui->tab->setUrl(QUrl(s));
//        if(st.isAdded(QUrl(s).toString())){
//                    ui->favorite->setIcon(QIcon(":/resource/imgs/add_to_favorites_52px.png"));
//                    st.isadd=1;
//                }
        qDebug()<<"url is"<<QString(ui->urlLine->text());
    });






    connect(ui->tab, &TabWidget::urlChanged, [=](const QUrl &url) {
        ui->urlLine->setText(url.toDisplayString());
    });

    //功能弹窗相关
    setuppopwidget();
    setuppopwidget2();
    setupsettingwidget();

    //下载相关
    setupDownload();

    //用户登录、管理
    setuploginwidget();
    //connect(ui->user,&QPushButton::clicked,this,&browserview::on_btn_user_clicked);

    // UA修改
    connect(settingWidget,&settingwidget::setUA,this,&browserview::changeUA);
    //初始化广告拦截
    ADinterceptor=new RequestInterceptor;
    connect(settingWidget,&settingwidget::setADBlock,this,&browserview::changeADBlock);


}


void browserview::changeUA(int UA)
{
        UAWidget::setUserAgent(ui->tab,UA);
        ui->tab->renew();
}
void browserview::changeADBlock(bool on)
{
    QWebEngineProfile *adblockProfile=ui->tab->getProfile();
    if(on){

        adblockProfile->setUrlRequestInterceptor(ADinterceptor);
    }else
    {
        adblockProfile->setUrlRequestInterceptor(NULL);
    }
        ui->tab->renew();
}


//设置弹窗
void browserview::setupsettingwidget()
{
    settingWidget = new settingwidget(this);
    settingWidget->setVisible(false);
    settingWidget->view = this;

    connect(ui->setting, &QPushButton::clicked, this, &browserview::changepopupwindowVisibility);
    popupwindow->addsettingwidget(settingWidget);
    connect(ui->setting, &QPushButton::clicked, this, &browserview::changesettingwidgetVisibility);
}

//用户弹窗
void browserview::setuploginwidget()
{
    setloginWidget = new Login(this);
    setloginWidget->setVisible(false);
    setloginWidget->view = this;

    setconfWidget = new UserConf(this);
    setconfWidget->setVisible(false);
    setconfWidget->view = this;

    connect(setloginWidget,&Login::closewin,this,&browserview::changepopupwindow2Visibility);


    connect(ui->user, &QPushButton::clicked, this, &browserview::changepopupwindow2Visibility);
    popupwindow2->addloginwidget(setloginWidget);
    //popupwindow2->addloginwidget(setconfWidget);
    connect(ui->user, &QPushButton::clicked, this, &browserview::changeloginwidgetVisibility);
}

void browserview::changesettingwidgetVisibility()
{
    if(settingWidget->isHidden())
    {
        settingWidget->setVisible(true);
    }


}
void browserview::changeloginwidgetVisibility()
{
    if(ACCOUNT.isEmpty())
    {
        if(setloginWidget->isHidden())
        {
            setloginWidget->setVisible(true);
        }
    }
    else
    {
        setconfWidget = new UserConf(this,ACCOUNT);
        popupwindow2->addloginwidget(setconfWidget);
        connect(setconfWidget,&UserConf::closewin,this,&browserview::changepopupwindow2Visibility);
        if(setconfWidget->isHidden())
        {
            setconfWidget->setVisible(true);
        }
    }
}

void browserview::setuppopwidget()
{
    popupwindow = new popwidget(this);
    popupwindow->setVisible(false);
    popupwindow->view = this;
}
void browserview::setuppopwidget2()
{
    popupwindow2 = new popwidget(this);
    popupwindow2->setVisible(false);
    popupwindow2->view = this;
}

void browserview::changepopupwindowVisibility()
{
    if(popupwindow->isHidden())
    {
        popupwindow->setGeometry(this->geometry().width()-395, 45, 340, 500);
        popupwindow->setVisible(true);
    }
    else
        popupwindow->hide();

}

void browserview::changepopupwindow2Visibility()
{
    if(popupwindow2->isHidden())
    {
        popupwindow2->setGeometry(this->geometry().width()-395, 45, 340, 500);
        popupwindow2->setVisible(true);
    }
    else
        popupwindow2->hide();

}

void browserview::setupDownload()
{
    downloadManager = new downloadManagerWidget(this);
    downloadManager->setVisible(false);
    downloadManager->view = this;
    //connect(downloadLocation, &QAction::triggered, this, &browserview::changeDownloadLocation);
    connect(ui->download, &QPushButton::clicked, this, &browserview::changeDownloadManagerWidgetVisibility);
    connect(QWebEngineProfile::defaultProfile(), &QWebEngineProfile::downloadRequested, downloadManager, &downloadManagerWidget::downloadRequested);
}



void browserview::changeDownloadManagerWidgetVisibility()
{
    if(downloadManager->isHidden())
    {
        downloadManager->setGeometry(this->geometry().width()-395, 45, 340, 500);
        downloadManager->setVisible(true);
    }
    else
        downloadManager->hide();

}

//void browserview::on_btn_user_clicked()
//{
//    if(ACCOUNT.isEmpty())
//    {
//        Login * lg = new Login(this);
//        lg->show();
//    }
//    else
//    {
//        UserConf * uc = new UserConf(this);
//        uc->show();
//    }

//}

browserview::~browserview()
{
    delete ui;
}

TabWidget *browserview::tabWidget() const
{
    return ui->tab;
}

WebView *browserview::currentTab() const
{
    return ui->tab->currentWebView();
}

void browserview::on_favorite_clicked(){

    st.on_button_addstar_clicked();
}

void browserview::initMenu(){
    m_MoreMenu=new QMenu(this);//主菜单
    connect(ui->toolButton_star,&QToolButton::clicked,this,&browserview::moreStar);

    m_morestar=new QAction("查看更多收藏页面");
    m_MoreMenu->addAction(m_morestar);
    connect(m_morestar,&QAction::triggered,this,&browserview::openStar_net);
    for(int i=0;i<5;i++)
    {
        m_liststar[i]=new QAction();
        m_MoreMenu->addAction(m_liststar[i]);
        connect(m_liststar[i],&QAction::triggered,this,[=](){
            this->url_for_open(i);
        });
    }
}

void browserview::moreStar()
{

       QPoint pos(this->geometry().x()+652*this->width()/800, this->geometry().y()+26);
       loaddata();
       m_MoreMenu->exec(pos);


}

void browserview::updateData(QString url){
    ui->tab->createTab();
    QString s=url;
    if(s.first(4)!="http")
    {
        s="http://"+s;
    }
    ui->tab->setUrl(QUrl(s));
    st.isAdded(s);
}

void browserview::openStar_net(){
    star_net=new Star_net(this);
    connect(star_net,SIGNAL(sendData(QString)),this,SLOT(updateData(QString)));
        if(star_net->exec()==QDialog::Accepted){
        }
    delete star_net;

}

void browserview::loaddata(){
    st.loadstar();
    for(int i=0;i<5;i++)
    {
        m_liststar[i]->setText(st.liststar[i]->text);
    }
}

void browserview::url_for_open(int i){
    ui->tab->createTab();
    QString s=st.liststar[i]->url;
    if(s.first(4)!="http")
    {
        s="http://"+s;
    }
    ui->tab->setUrl(QUrl(s));
    st.isAdded(s);

}




void browserview::updateDataHR(QString url){
    ui->tab->createTab();
    QString s=url;
    if(s.first(4)!="http")
    {
        s="http://"+s;
    }
    ui->tab->setUrl(QUrl(s));
}

void browserview::on_history_clicked()
{
        HistoryRecord *HR=new HistoryRecord();
        ///
        connect(HR,SIGNAL(sendData(QString)),this,SLOT(updateDataHR(QString)));
        ///
        HR->InitTable();
        HR->show();
}



